package club.xyes.zkh.ecommerce.web.api.controller;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.commons.entity.UserCollectShop;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.EntityImagePathUtils;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.ShopService;
import club.xyes.zkh.ecommerce.service.UserCollectShopService;
import club.xyes.zkh.ecommerce.service.UserService;
import club.xyes.zkh.ecommerce.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Create by 郭文梁 2019/4/18 0018 14:11
 * UserCollectShopController
 * 用户收藏商铺相关数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@RestController
@RequestMapping("/api/user-collect-shop")
public class UserCollectShopController extends BaseController<UserCollectShop> {
    private final UserCollectShopService service;
    private final ShopService shopService;
    private final UserService userService;
    private final WebsiteConfigurationProperties websiteConfigurationProperties;

    @Autowired
    public UserCollectShopController(UserCollectShopService service,
                                     ShopService shopService,
                                     UserService userService,
                                     WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(service);
        this.service = service;
        this.shopService = shopService;
        this.userService = userService;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    /**
     * 创建收藏记录
     *
     * @param param {id:shopId}
     * @return GR
     */
    @PostMapping("/collect")
    @ApiOperation("用户收藏店铺")
    public GeneralResult<UserCollectShop> collect(@RequestBody Shop param) {
        if (param == null || param.getId() == null) {
            return GeneralResult.badRequest("缺少参数");
        }
        Integer shopId = param.getId();
        Shop shop = shopService.getRequired(shopId);
        User user = userService.getRequired(getRequiredUserInfo().getUserId());
        UserCollectShop log = service.collect(user, shop);
        EntityImagePathUtils.prettyLogoPath(log.getShop(),
                websiteConfigurationProperties.getStaticServerPrefix(),
                websiteConfigurationProperties.getShopLogoPath());
        return GeneralResult.ok(log);
    }

    /**
     * 获取当前用户的收藏记录
     *
     * @param page 页码
     * @param rows 每页大小
     * @return GR with pageinfo
     */
    @GetMapping("/mine")
    @ApiOperation("获取当前用户的收藏记录")
    public GeneralResult<PageInfo<UserCollectShop>> mine(Integer page, Integer rows) {
        page = page == null ? DEFAULT_PAGE : page;
        rows = rows == null ? DEFAULT_ROWS : rows;
        PageInfo<UserCollectShop> logs = service.findByUserId(getRequiredUserInfo().getUserId(), page, rows);
        logs.setList(logs.getList().stream()
                .peek(v -> EntityImagePathUtils.prettyLogoPath(v.getShop(),
                        websiteConfigurationProperties.getStaticServerPrefix(),
                        websiteConfigurationProperties.getShopLogoPath())).collect(Collectors.toList()));
        return GeneralResult.ok(logs);
    }

    /**
     * 取消收藏记录
     *
     * @param id ID
     * @return GR
     */
    @DeleteMapping("/{id}")
    @ApiOperation("取消收藏")
    public GeneralResult<UserCollectShop> delete(@PathVariable("id") Integer id) {
        UserCollectShop log = service.getRequired(id);
        if (!Objects.equals(log.getUserId(), getRequiredUserInfo().getUserId())) {
            return GeneralResult.permissionDenied("您无权操作该数据");
        }
        if (service.deleteById(log.getId())) {
            EntityImagePathUtils.prettyLogoPath(log.getShop(),
                    websiteConfigurationProperties.getStaticServerPrefix(),
                    websiteConfigurationProperties.getShopLogoPath());
            return GeneralResult.ok(log);
        } else {
            return GeneralResult.error("Unable to delete UserCollectShopLog for id " + id);
        }
    }

    /**
     * 取消收藏
     *
     * @param shopId 商铺ID
     * @return GR
     */
    @ApiOperation("取消收藏")
    @DeleteMapping("/shop/{shopId}")
    public GeneralResult<UserCollectShop> uncollect(@PathVariable("shopId") Integer shopId) {
        User user = currentUser(userService);
        Shop shop = shopService.getRequired(shopId);
        UserCollectShop res = service.unCollect(user, shop);
        return GeneralResult.ok(res);
    }
}
